<template>
  <div class="login-content">
    <!-- 登录表单 -->
    <div class="login_box">
      <el-form
        class="login_form"
        ref="login_ref"
        :model="LoginForm"
        :rules="login_rules"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
            v-model="LoginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            v-model="LoginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 头像 -->
    <div class="login-avatar">
      <img src="../assets/login.jpeg" alt="" />
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      LoginForm: {
        username: "admin",
        password: "123456",
      },
      login_rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    // 重置按钮
    reset() {
      this.$refs.login_ref.resetFields();
    },
    login() {
      this.$refs.login_ref.validate(async (valid) => {
        // console.log(valid);
        if (!valid) return;
        await this.$http.post("login", this.LoginForm).then((res) => {
          // console.log(res.data);
          if (res.data.meta.status != 200)
            return this.$message.error("登录失败");
          this.$message.success("登录成功");
          // 保存token
          window.sessionStorage.setItem("token", res.data.data.token);
          this.$router.push("/home");
        });
      });
    },
  },
};
</script>
<style lang="less" scoped>
.login-content {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  background-color: #fff;
  width: 450px;
  height: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -150px;
  margin-left: -225px;
  border-radius: 3px;
  // padding-top: 50px;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
  .btns {
    display: flex;
    justify-content: space-around;
  }
}
.login-avatar {
  height: 200px;
  width: 200px;
  border-radius: 50%;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 30%;
  margin: -100px;
  overflow: hidden;
  border: 5px solid #ccc;
  box-shadow: 0 0 10px #ddd;
  img {
    width: 100%;
    height: 100%;
    background-color: #ccc;
  }
}
</style>
